home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / mnpc12.zip / TIMER.ASM < prev   
Assembly Source File  |  1988-01-30  |  5KB  |  192 lines

  1.     title    timers.. timerins, timerrem, and timerint
  2.     page    60,132
  3. ;==============================================================================
  4. ;
  5. ;                   The Microcom MNP Library
  6. ;                    (Microsoft C Version)
  7. ;------------------------------------------------------------------------------
  8. ;
  9. ;    timerins -- timer install routine
  10. ;
  11. ;    synopsis: timerins();
  12. ;
  13. ;------------------------------------------------------------------------------
  14. ;
  15. ;    timerrem -- timer remove routine
  16. ;
  17. ;    synopsis: timerrem();
  18. ;
  19. ;------------------------------------------------------------------------------
  20. ;
  21. ;    timerint -- timer interrupt routine (invoked on timer tick)
  22. ;
  23. ;------------------------------------------------------------------------------
  24. ;
  25. ;    Modification History
  26. ;
  27. ;    10/29/87 - timerint allowed to run with interrupts enabled..gp
  28. ;
  29. ;==============================================================================
  30.  
  31. _data    segment word public 'DATA'
  32. _data   ends
  33. dgroup  group    _data
  34.  
  35. _data   segment
  36. old_t_vct    label    dword        ; old timer vector contents
  37. old_bx        dw    0        ; base
  38. old_es        dw    0        ; segment
  39. timer_active    db    0        ; timer active flag
  40. tick        db    18        ; clock tick count
  41.  
  42. timer_no  equ    6            ; number of timers in table
  43.  
  44.     extrn    _g_tm:word    ; start of timers 
  45.  
  46. _data    ends
  47.  
  48. _text    segment byte public 'CODE'
  49.     assume    cs:_text,ds:dgroup
  50.  
  51.     public    _timerins,_timerrem
  52.     public    cdsg
  53.     public     _timer_int
  54.  
  55.  
  56. ; Data in code segment
  57. cdsg    dw    0
  58.  
  59.     page
  60. ;PUBLIC***********************************************************************
  61. ;
  62. ;    timerins - timer installation routine
  63. ;
  64. ;*****************************************************************************
  65.  
  66. _timerins proc near
  67.  
  68.     push    bp
  69.     push    es                ; save es and ds
  70.     push    ds
  71.  
  72. ; Check to be sure that the timer interrupt handler is not already
  73. ; installed.  Do nothing in this case.
  74.     cmp    timer_active,1        ; already installed?
  75.     je    tin9                ; yes-go return
  76.  
  77. ; Initialize
  78.     mov    timer_active,1        ; set timer active flag
  79.     mov    cs:cdsg,ds        ; save data segment
  80.  
  81. ; Save current interrupt vector contents then install our own handler.
  82.     mov    ah,35h            ; get vector request
  83.     mov    al,1ch            ; we want the timer one
  84.     int    21h                ; int to DOS
  85.     mov    old_bx,bx            ; save vector contents
  86.     mov    old_es,es
  87.     mov    dx,offset _timer_int    ; point to our handler
  88.     mov    ax,cs            ; be sure ds is right
  89.     mov    ds,ax
  90.     mov    ah,25h            ; set vector request
  91.     mov    al,1ch            ; we want the timer one
  92.     int    21h                ; int to DOS
  93.  
  94. ; Return
  95. tin9:
  96.     pop    ds                ; restore data segment
  97.     pop    es                ; restore es
  98.     pop    bp
  99.     ret
  100.  
  101. _timerins endp
  102.      page
  103. ;PUBLIC***********************************************************************
  104. ;
  105. ;    timerrem - timer removal subroutine
  106. ;
  107. ;*****************************************************************************
  108.  
  109. _timerrem proc near
  110.  
  111.     push    bp
  112. ; Check to be sure that the timer interrupt handler is already
  113. ; installed.  Do nothing if not.
  114.     cmp    timer_active,1        ; installed?
  115.     jne    tir9                ; no-go return
  116.  
  117. ; Recover old vector contents and put them back.
  118.     push    ds                ; save ds register
  119.     mov    ah,25h            ; set vector request
  120.     mov    al,1ch            ; we want the timer one
  121.     mov    dx,old_bx            ; recover old vector
  122.     mov    ds,old_es
  123.     int    21h                ; int to DOS
  124.     pop    ds                ; restore ds register
  125.     mov    timer_active,0        ; reset timer active flag
  126.  
  127. ; Return
  128. tir9:
  129.     pop    bp
  130.     ret
  131.  
  132. _timerrem endp
  133.     page
  134. ;*****************************************************************************
  135. ;
  136. ;    timerint - timer interrupt routine
  137. ;
  138. ;*****************************************************************************
  139.  
  140. _timer_int proc far
  141.  
  142. ; Initialize
  143.     push    ds                ; save registers
  144.     mov    ds,cs:cdsg        ; enable data addressability
  145.  
  146. ; Re-enable interrupts
  147. ;####    sti
  148.  
  149. ; When a clock interrupt occurs, decrement the second ticker.  If the
  150. ; ticker goes to zero, count down the second-based timers.
  151. t1:
  152.     dec    tick                ; decrement ticker
  153.     jnz    tiret            ; go on if not zero
  154.  
  155. ; Pass through second-based timers and decrement any that are not already
  156. ; zero.  Zero can mean either 'elapsed' or 'inactive'.
  157. ti1:
  158.     mov    tick,18             ; reset ticker
  159.  
  160.     push    bx                ; save registers
  161.     push    cx
  162.     mov     bx,offset _g_tm
  163. ;    mov    bx,offset timers    ; point to list of timers
  164.     mov    cx,timer_no        ; do for each timer
  165.  
  166. ti3:
  167.     cmp    word ptr [bx],0     ; active?
  168.     je    ti4                ; no-go on
  169.     dec    word ptr [bx]        ; yes-count it down
  170.  
  171. ti4:
  172.     add    bx,2                ; advance to next timer
  173.     loop    ti3                ; and try again
  174.  
  175.     pop    cx                ; restore registers
  176.     pop    bx                ; and go on
  177.  
  178. tiret:
  179.     cli                    ;  disable interrupts before call
  180.     pushf                ; this is to simulate an int instruction
  181.     call    old_t_vct            ; inter-segment indirect call
  182.  
  183.     pop    ds
  184.     sti                    ; re-enable interrupts
  185.     iret                    ; and return from interrupt
  186.  
  187. _timer_int endp
  188.  
  189. _text    ends
  190.     end
  191.